﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using HHL_Acc.HHL_DaoAcc.DataObject;
using HHL_Acc.HHL_EntityAcc;
using HHL_DaoAcc.DataObject;
using System.Data;

namespace HHL_Acc.HHL_BussAcc.DataObject
{
    public class SmiImportsBus
    {
        private SmiImportsDao smiImportsDao ;

        public SmiImportsBus()
        {
            smiImportsDao = new SmiImportsDao();
        }

        public DataTable GetListSmiImport()
        {
            return smiImportsDao.GetAllSmiImports();
        }

        public SmiImports GetImports(String importId)
        {
            return smiImportsDao.GetAllSmiImportsById(importId);
        }

        public DataTable GetListSmiImport(int? imNumber, int? year, DateTime dateFrom, DateTime dateTo)
        {
            return smiImportsDao.GetSmiImportsWithCondition(imNumber, year, dateFrom, dateTo);
        }

        public int InsertSmiImport(SmiImports dto, List<SmiImportDetails> smiImportDetailses)
        {
            
            // Insert table Smi Import Details
            int result = smiImportsDao.InsertSmiImports(dto);
            if (result <= 0)
            {
                // return if insert unsuccessful
                return result;
            }
            SmiImportDetailsDao smiImportDetailsDao = new SmiImportDetailsDao();
            if (smiImportDetailses != null && smiImportDetailses.Count>0)
            {

                foreach (SmiImportDetails smiImportDetail in smiImportDetailses)
                {
                    smiImportDetailsDao.InsertSmiImportDetail(smiImportDetail);
                }
            }
            return 1;
            //smiImportDetailsDao.
        }

        public List<SmiImports> GetListSmiImportL()
        {
            return (List<SmiImports>) smiImportsDao.GetAllSmiImportsL();
        }

        public int UpdateSmiImport(SmiImports smiImports, List<SmiImportDetails> smiImportDetailsList)
        {
            // Insert table Smi Import Details
           // SqlTransaction sqlTransaction = new SqlTransaction()

            int result = smiImportsDao.UpdateSmiImports(smiImports);
            if (result <= 0)
            {
                // return if insert unsuccessful
                return result;
            }
            SmiImportDetailsDao smiImportDetailsDao = new SmiImportDetailsDao();

            // delete all old record
            int deleteRslt =  smiImportDetailsDao.DeleteSmiImportDetailByImpId(smiImports.ImpId);
            if (deleteRslt == -1)
            {
                return -1;
            }
            // insert new 
            if (smiImportDetailsList != null && smiImportDetailsList.Count > 0)
            {

                foreach (SmiImportDetails smiImportDetail in smiImportDetailsList)
                {
                   result = smiImportDetailsDao.InsertSmiImportDetail(smiImportDetail);
                   if (result <= 0)
                   {
                       // return if insert unsuccessful
                       return result;
                   }
                }
            }
            return 1;
        }
    }
}